@Echo off
Echo --------------------------------------------------------------------------------
Echo %~f0 %*

:: File: C:\Documents and Settings\paul\My Documents\Src\Batch\set_JAVA.cmd
:: Date: 10:56 PM 12/19/2010

:: Set_XXX 
:: given an environment variable, and a list of path/choice options,
:: find the user's choice and set the environment variable to the matching path.
:: the idea is for something like:
::    JAVA_HOME
::    C:\Program Files\Java
::       j2re1.4.2_03
::       jdk1.6.0_21
::       jre1.5.0_02
:: .... etc ....

:: The user should be able to pick jre1.6.0_07, 1.6.0_07, or 16007 and get
:: JAVA_HOME set to C:\Program Files\Java\jre1.6.0_07

:: Requires the current environment to have DelayedExpansion and 
:: CommandExtensions enabled for Current User {HKCU} or Local Machine {HKLM}:
::    reg add "HKCU\Software\Microsoft\Command Processor" /V EnableExtensions  /T REG_DWORD  /d 0x1 /f
::    reg add "HKCU\Software\Microsoft\Command Processor" /V DelayedExpansion  /T REG_DWORD  /d 0x1 /f
    
Set JAVA_HOME=
Set JAVACMD=

If /I "%1" equ "latest" "%~0" 16021
Call :ChkJava "%~1"   jre6 1.6.0_22 16022  jdk1.6.0_21 1.6.0_21 16021  jre1.6.0_07 1.6.0_07 16007  jre1.6.0_05 1.6.0_05 16005  jre1.6.0_03 1.6.0_03 16003  jre1.6.0_02 1.6.0_02 16002  jre1.6.0_01 1.6.0_01 16001  jre1.5.0_11 1.5.0_11 15011  jre1.5.0_10 1.5.0_10 15010  jre1.5.0_09 1.5.0_09 15009  jre1.5.0_06  1.5.0_06 15006   jre1.5.0_04 1.5.0_04 15004  jre1.5.0_02 1.5.0_02 15002  j2re1.4.2_03 1.4.2_03 14203

Exit /B

:: ----------------------------------------------------------------------------
:: ----------------------------------------------------------------------------

:: ChkJava Params:
::    User-supplied cmd line parm, 
::    triplets of directory/version tags.
::       The first tag of the triplet must be the actual directory name
::       The second and third tags are aliases

:ChkJava
Set PARMS=

:ChkJavaLoop
Set PARMS=!PARMS! %2 or %3 or %4 ;
If /I "%~1" equ "%~2" Set JAVA_HOME=C:\Program Files\Java\%~2
If /I "%~1" equ "%~3" Set JAVA_HOME=C:\Program Files\Java\%~2
If /I "%~1" equ "%~4" Set JAVA_HOME=C:\Program Files\Java\%~2
:: If first tag of next triplet is blank, we are done.
:: If we found a HOME, set stuff dependent on HOME.
If "%~5"=="" (
   If "!JAVA_HOME!"=="" (
      Echo.
      Echo Looking for a valid JAVA version:
      Echo !PARMS!
   ) Else (
      Set JAVACMD=!JAVA_HOME!\bin\java.exe
      Echo JAVA_HOME=!JAVA_HOME!
      Echo JAVACMD=!JAVACMD!
      "!JAVACMD!" -version
   )
   Exit /B
)
:: Keep %1, scroll next triplet into %2 %3 %4
Shift /2
Shift /2
Shift /2 
Goto :ChkJavaLoop

